package com.controller
{
	import com.events.UserMasterEvent;
	import com.model.valueObjects.VOUserMaster;
	import com.service.UserMasterDetails;
	
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.Base64Decoder;

	public class UserMasterController extends AbstractController
	{
		import com.model.Model;
		[Inject]
		public var userMasterDetails:UserMasterDetails;
		public function UserMasterController()
		{
			super();
		}

		
		[EventHandler(event="UserMasterEvent.GET_ALL")]
		public function getAllUserMasterController():void{
		sh.executeServiceCall(userMasterDetails.getAllUsers(), getAllUserResult, handleFault );
		}
		
		private function getAllUserResult(event:ResultEvent):void
		{
			model.UserMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.currentButtonIndex=0;
		}
		[EventHandler(event="UserMasterEvent.GET_ALL_CLIENT")]
		public function getAllClientMasterController():void{
			sh.executeServiceCall(userMasterDetails.getAllClients(), getAllClientResult, handleFault );
		}
		
		private function getAllClientResult(event:ResultEvent):void
		{
			model.ClientMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="UserMasterEvent.ADD", properties="userVo")]
		public function addUser(userVo:VOUserMaster):void
		{
			sh.executeServiceCall(userMasterDetails.addNewUser(userVo), addUserResult, handleFault );	
		}
		private function addUserResult(event:ResultEvent):void
		{
			Alert.show("New User Details added");
			model.currentButtonIndex=0;
			getAllUserMasterController();
			getAllClientMasterController();
		}
		[EventHandler(event="UserMasterEvent.UPDATE", properties="userVo")]
		public function editUser(userVo:VOUserMaster):void
		{
			sh.executeServiceCall(userMasterDetails.editUserDetails(userVo), updateUserResult, handleFault );	
		}
		private function updateUserResult(event:ResultEvent):void
		{
			model.currentButtonIndex=0;
			Alert.show("User Details Updated");
			getAllUserMasterController();
		}
		[EventHandler(event="UserMasterEvent.DELETE", properties="clientId")]
		public function deleteUser(clientId:String):void
		{
			sh.executeServiceCall(userMasterDetails.deleteUserDetails(clientId), deleteUserResult, handleFault );	
		}
		private function deleteUserResult(event:ResultEvent):void
		{
			Alert.show("User Deleted");
			getAllUserMasterController();
		}
		[EventHandler(event="UserMasterEvent.CHANGE_PASSWORD", properties="password,clientId")]
		public function changePassword(password:String,clientId:String):void
		{
			sh.executeServiceCall(userMasterDetails.changeUserPassword(password,clientId), changePasswordResult, handleFault );	
		}
		private function changePasswordResult(event:ResultEvent):void
		{
			Alert.show("Password Changed");
			CursorManager.removeBusyCursor();
		}
		private function handleFault(event:FaultEvent):void
		{
			//Alert.show( 'v '+event.fault.name, "Error" );
			CursorManager.removeBusyCursor();
		}
		
		
	}
}